package org.odk.clinic.android.activities;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.odk.clinic.android.R;
import org.odk.clinic.android.database.ClinicAdapter;
import org.odk.clinic.android.database.SQLiteAdapter;
import org.odk.clinic.android.openmrs.Constants;
import org.odk.clinic.android.openmrs.Patient;
import org.odk.clinic.android.openmrs1.person_name;

import custom_View.aperson;





import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class DetailPatientActivity extends TabActivity implements TabHost.TabContentFactory{
	private SimpleDateFormat mDateFormat = new SimpleDateFormat("dd-mm-yyyy");
	TextView tv1,tv2,tv3,tv4,tv5,tv6;
	private SQLiteAdapter mDB;
	private aperson p = new aperson();
	Cursor mCursor;
	private String name;
	static int mid;
	private person_name pna = new person_name();
	
	protected void onCreate(Bundle ici){
		super.onCreate(ici);
		setContentView(R.layout.view_patient_tab);
		//tv1 = (TextView)this.findViewById(R.id.detailpatient);
		//tv2 = (TextView)this.findViewById(R.id.detailpatientadress);
		/////////
		
		
		///
		Resources res =getResources();
		TabHost.TabSpec spec;
		Intent i,j,k ;
		i = new Intent(DetailPatientActivity.this,ViewEncounterActivity.class);
		j = new Intent(DetailPatientActivity.this,ViewProblemActivity.class);
		k = new Intent(DetailPatientActivity.this,ViewObservationActivity.class);
		
		TabHost tabHost = getTabHost();
		spec = tabHost.newTabSpec("overview").setIndicator("Overview",res.getDrawable(R.drawable.overview_patient)).setContent(j);
		tabHost.addTab(spec);
		spec = tabHost.newTabSpec("regimens").setIndicator("Regimens").setContent(k);
		tabHost.addTab(spec);
		spec = tabHost.newTabSpec("encounter").setIndicator("Encounter").setContent(i);
		tabHost.addTab(spec);
		tabHost.setCurrentTab(0);
		tv1 = (TextView)this.findViewById(R.id.text1);
		tv2 = (TextView)this.findViewById(R.id.text2);
		tv3 = (TextView)this.findViewById(R.id.text3);
		tv5 = (TextView)this.findViewById(R.id.text5);
		tv6 = (TextView)this.findViewById(R.id.text6);
		
				mDB = new SQLiteAdapter(this);
				mDB.open();
			
				mid = ListPatientActivity2.mid;
				mCursor = mDB.getPerson(mid);
				if(mCursor.moveToFirst()){
					do{
						StringBuilder name = new StringBuilder();
						name.append(mCursor.getString(mCursor.getColumnIndex(SQLiteAdapter.KEY_GIVENNAME)));
						name.append(" ");
						name.append(mCursor.getString(mCursor.getColumnIndex(SQLiteAdapter.KEY_MIDLENAME)));
						name.append(" ");
						name.append(mCursor.getString(mCursor.getColumnIndex(SQLiteAdapter.KEY_FAMILYNAME)));
						tv1.setText(name.toString());
						tv2.setText(mCursor.getString(mCursor.getColumnIndex(SQLiteAdapter.KEY_PATIENT_ID)).toString());
						ImageView imageView = (ImageView)findViewById(R.id.image_genger);
						if (imageView != null) {
							if (mCursor.getString(mCursor.getColumnIndex(SQLiteAdapter.KEY_GENDER)).equals("M")) {
								imageView.setImageResource(R.drawable.male);
							} else if (mCursor.getString(mCursor.getColumnIndex(SQLiteAdapter.KEY_GENDER)).equals("F")) {
								imageView.setImageResource(R.drawable.female);
							}
						}
						
						tv3.setText(mCursor.getString(mCursor.getColumnIndex(SQLiteAdapter.KEY_BIRTH)));
						//////////////////////////
						tv5.setText(mCursor.getString(mCursor.getColumnIndex(SQLiteAdapter.KEY_PERSON_NAME_EMAIL)));
						tv6.setText(mCursor.getString(mCursor.getColumnIndex(SQLiteAdapter.KEY_PERSON_NAME_PHONE)));
						
					}
					while (mCursor.moveToNext());
				}
				
				tv5.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent sendIntent = new Intent(android.content.Intent.ACTION_SENDTO);
						sendIntent.setType("text/html");
						sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "testing email send.");
						sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<b>this is html text in email body.</b>"));
						startActivity(Intent.createChooser(sendIntent, "Email to Friend"));
						//Intent sendIntent = new Intent(Intent.ACTION_VIEW);
						//sendIntent.putExtra("sms_body", "The SMS text"); 
						//sendIntent.setType("vnd.android-dir/mms-sms");
						//startActivity(sendIntent); 
					
					}
				});
				tv6.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						TextView cal = (TextView)findViewById(R.id.text6);
						Intent dialIntent = new Intent(Intent.ACTION_CALL);
						dialIntent.setData(Uri.parse("tel:" + cal.getText()));
						startActivity(dialIntent); 
					
					}
				});
			/*	String patientIdStr = getIntent().getStringExtra("id");
				Integer patientId = Integer.valueOf(patientIdStr);
				
				//TextView tv1 = (TextView)this.findViewById(R.id.text1);
				//tv1.setText(patientIdStr);
				//getPatient(patientId);
				TextView tv1 = (TextView)this.findViewById(R.id.text1);
				tv1.setText(mCursor.getString(mCursor.getColumnIndex(mDB.KEY_GIVENNAME)));*/
			}
	   
	@Override
	public View createTabContent(String tag){
		LayoutInflater inflater = getLayoutInflater();
		View convertView = null;
		if(tag.equals("overview")){
			convertView = inflater.inflate(R.layout.detail_patient, null,false);
		}
		if(tag.equals("regimens")){
			convertView = inflater.inflate(R.layout.list_observation_item, null,false);
		}
		if(tag.equals("encounter")){
			convertView = inflater.inflate(R.layout.list_encounter_item, null,false);
		}
				
		return convertView;
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.detaimenu, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case R.id.addencounter:
			Intent i = new Intent(this,NewEncounterActivity1.class);
			startActivity(i);break;
		case R.id.addproblem:
			Intent j = new Intent(this, ProblemActivity.class);
			startActivity(j);break;
		case R.id.addRegime :
			Intent k = new Intent(this,NewObservationActivity.class);
			startActivity(k);break;
		}
		return true;
	}
	

}
